const { sequelize } = require('../../core/db')
const { Sequelize, Model } = require('sequelize')


class Book extends Model{
    // 图书搜索
    static async searchFromYuShu(q, start, count, summary = 1) {
        // encodeURI把有中文的转义掉
        const url = util.format(global.config.yushu.keywordUrl, encodeURI(q), count, start, summary)
        const detail = await axios.get(url)
        return detail.data
    }

    // 获取我喜欢的书籍数量
    static async getMyFavorBookCount(uid) {
        const count = await Favor.count({
            where: {
                type: 400,
                uid
            }
        })
        return count
    }
}

Book.init({
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    fav_nums: {
        type: Sequelize.INTEGER,
        defaultValue: 0
    }
}, {
    sequelize,
    tableName: 'book'
})

module.exports = {
    Book
}
